package cn.edu.hunan.pm.common;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.List;

/**
 * @author hb
 * @date 2024-06-20 10:30:58
 */
public class ParseBindingResult {

    public static String parse(BindingResult bindingResult) {
        StringBuilder sb = new StringBuilder();
        if (bindingResult!=null && bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();

            fieldErrors.forEach(fe -> {
                String errMessage = fe.getDefaultMessage();
                sb.append(errMessage);
                sb.append(";");
            });

            return sb.substring(0, sb.length() -1);
        }
        return null;
    }
}
